﻿using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using MaterialDesignThemes.Wpf;

namespace config.view
{
    public enum MessageBoxType
    {
        Info,
        Success,
        Warning,
        Error,
        Question,
        Delete
    }

    public partial class MyMessageBox : Window
    {
        public bool Result { get; private set; }

        public MyMessageBox(string message, string title = "提示", MessageBoxType type = MessageBoxType.Question)
        {
            InitializeComponent();
            MessageText.Text = message;
            TitleText.Text = title;

            // 根据类型设置样式
            SetMessageBoxType(type);

            // 设置窗口拖动
            this.MouseDown += Window_MouseDown;
        }

        private void SetMessageBoxType(MessageBoxType type)
        {
            switch (type)
            {
                case MessageBoxType.Info:
                    SetInfoStyle();
                    break;
                case MessageBoxType.Success:
                    SetSuccessStyle();
                    break;
                case MessageBoxType.Warning:
                    SetWarningStyle();
                    break;
                case MessageBoxType.Error:
                    SetErrorStyle();
                    break;
                case MessageBoxType.Question:
                    SetQuestionStyle();
                    break;
                case MessageBoxType.Delete:
                    SetDeleteStyle();
                    break;
            }
        }

        private void SetInfoStyle()
        {
            TitleBar.Background = new SolidColorBrush((Color)FindResource("InfoColor"));
            HeaderIcon.Foreground = Brushes.White;
            HeaderIcon.Kind = PackIconKind.Information;
            TitleText.Foreground = Brushes.White;
            ConfirmButton.Background = new SolidColorBrush((Color)FindResource("InfoColor"));
            ConfirmButton.Content = "知道了";
        }

        private void SetSuccessStyle()
        {
            TitleBar.Background = new SolidColorBrush((Color)FindResource("SuccessColor"));
            HeaderIcon.Foreground = Brushes.White;
            HeaderIcon.Kind = PackIconKind.CheckCircle;
            TitleText.Foreground = Brushes.White;
            ConfirmButton.Background = new SolidColorBrush((Color)FindResource("SuccessColor"));
            ConfirmButton.Content = "好的";
        }

        private void SetWarningStyle()
        {
            TitleBar.Background = new SolidColorBrush((Color)FindResource("WarningColor"));
            HeaderIcon.Foreground = Brushes.White;
            HeaderIcon.Kind = PackIconKind.Alert;
            TitleText.Foreground = Brushes.White;
            ConfirmButton.Background = new SolidColorBrush((Color)FindResource("WarningColor"));
            ConfirmButton.Content = "继续";
        }

        private void SetErrorStyle()
        {
            TitleBar.Background = new SolidColorBrush((Color)FindResource("ErrorColor"));
            HeaderIcon.Foreground = Brushes.White;
            HeaderIcon.Kind = PackIconKind.Error;
            TitleText.Foreground = Brushes.White;
            ConfirmButton.Background = new SolidColorBrush((Color)FindResource("ErrorColor"));
            ConfirmButton.Content = "关闭";
        }

        private void SetQuestionStyle()
        {
            TitleBar.Background = new SolidColorBrush((Color)FindResource("QuestionColor"));
            HeaderIcon.Foreground = Brushes.White;
            HeaderIcon.Kind = PackIconKind.QuestionMark;
            TitleText.Foreground = Brushes.White;
            ConfirmButton.Background = new SolidColorBrush((Color)FindResource("QuestionColor"));
            ConfirmButton.Content = "确定";
        }

        private void SetDeleteStyle()
        {
            TitleBar.Background = new SolidColorBrush((Color)FindResource("ErrorColor"));
            HeaderIcon.Foreground = Brushes.White;
            HeaderIcon.Kind = PackIconKind.Delete;
            TitleText.Foreground = Brushes.White;
            ConfirmButton.Style = (Style)FindResource("DangerButtonStyle");
            ConfirmButton.Content = "删除";
        }

        private void ConfirmButton_Click(object sender, RoutedEventArgs e)
        {
            Result = true;
            this.DialogResult = true;
            this.Close();
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            Result = false;
            this.DialogResult = false;
            this.Close();
        }

        private void Window_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Left)
                this.DragMove();
        }

        // 静态方法方便调用
        public static bool Show(string message, string title = "提示", MessageBoxType type = MessageBoxType.Question)
        {
            var dialog = new MyMessageBox(message, title, type);
            dialog.Owner = Application.Current.MainWindow;
            dialog.ShowDialog();
            return dialog.Result;
        }

        // 专用方法
        public static void ShowInfo(string message, string title = "信息")
        {
            Show(message, title, MessageBoxType.Info);
        }

        public static bool ShowQuestion(string message, string title = "确认")
        {
            return Show(message, title, MessageBoxType.Question);
        }

        public static void ShowSuccess(string message, string title = "成功")
        {
            Show(message, title, MessageBoxType.Success);
        }

        public static void ShowWarning(string message, string title = "警告")
        {
            Show(message, title, MessageBoxType.Warning);
        }

        public static void ShowError(string message, string title = "错误")
        {
            Show(message, title, MessageBoxType.Error);
        }

        public static bool ShowDeleteConfirmation(string message, string title = "删除确认")
        {
            return Show(message, title, MessageBoxType.Delete);
        }
    }
}